﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using MathNet.Numerics.LinearAlgebra;

namespace GeometryMod.Models
{
    public class Tools3
    {

        public static bool GetIntersectionXYZ(Point3D pt1, Point3D pt2, Point3D pt3, Point3D pt4, out Point3D result)
        {
            result = new Point3D();
            Vector3 v1 = new Vector3((float)(pt2.X - pt1.X), (float)(pt2.Y - pt1.Y), (float)(pt2.Z - pt1.Z));
            Vector3 v2 = new Vector3((float)(pt4.X - pt3.X), (float)(pt4.Y - pt3.Y), (float)(pt4.Z - pt3.Z));
            Point3D intersection = new Point3D();
            bool resultDs = false;

            if (v1.X == 0 || v2.X == 0)
            {
                resultDs = GetIntersectionYZ(pt1,pt2,pt3,pt4,out intersection);
            }
            else if (v1.Y == 0 || v2.Y == 0)
            {
                resultDs = GetIntersectionXZ(pt1, pt2, pt3, pt4, out intersection);

            }
            else if (v1.Z == 0 || v2.Z == 0)
            {
                resultDs = GetIntersectionXY(pt1, pt2, pt3, pt4, out intersection);

            }
            else
            {

            }
            return false;
        }

        public static bool GetIntersectionXY(Point3D pt1, Point3D pt2, Point3D pt3, Point3D pt4, out Point3D result)
        {
            result = new Point3D();



            return false;
        }
        public static bool GetIntersectionXZ(Point3D pt1, Point3D pt2, Point3D pt3, Point3D pt4, out Point3D result)
        {
            result = new Point3D();



            return false;
        }

        public static bool GetIntersectionYZ(Point3D pt1, Point3D pt2, Point3D pt3, Point3D pt4, out Point3D result)
        {
            result = new Point3D();
            Vector3 v1 = new Vector3((float)(pt2.X - pt1.X), (float)(pt2.Y - pt1.Y), (float)(pt2.Z - pt1.Z));
            Vector3 v2 = new Vector3((float)(pt4.X - pt3.X), (float)(pt4.Y - pt3.Y), (float)(pt4.Z - pt3.Z));


            double a1 = 0.0;
            double b1 = 0.0;
            double c1 = 0.0;
            //L= P + tV
            //y = pt1.y + t*v1.y
            //z = pt1.z + t.v1.z

            double a2 = 0.0; 
            double b2 = 0.0;
            double c2 = 0.0;
            //y = pt3.y + s*v2.y
            //z = pt3.z + s.v2.z
            double t;
            double s;

            //y 联立
            //pt1.Y + t * v1.Y = pt3.Y + s * v2.Y;
            //pt1.Z + t * v1.Z = pt3.Z + s * v2.Z;
            //ax+by = c
            //v1.Y * t  -v2.Y * s  = pt3.Y-pt1.Y;
            //v1.Z * t  -v2.Z * s  = pt3.Z-pt1.Z;
            //解 t,s
            //系数 v1.Y,-v2.Y,pt3.Y-pt1.Y,v1.Z,-V2.Z,pt3.Z - pt1.Z


           var calaResult = SolveLinearEquations(v1.Y,-v2.Y,pt3.Y-pt1.Y,v1.Z,-v2.Z,pt3.Z-pt1.Z,out t,out s);
            result = new Point3D(pt1.X,pt1.Y + t*v1.Y,pt1.Z + t*v1.Z);
            return calaResult;
        }


        public static bool Solve(
      double a1, double b1, double c1,
      double a2, double b2, double c2,out PointF interPoint)
        {
            interPoint = new PointF();
            // 构建主矩阵 
            var mainMatrix = Matrix<double>.Build.DenseOfArray(new[,] {
            { a1, b1 },
            { a2, b2 }
        });

            // 计算主行列式
            double D = mainMatrix.Determinant();
            if (Math.Abs(D) < 1e-12)
                return false;

            // 构建替换矩阵 
            var matrixX = Matrix<double>.Build.DenseOfArray(new[,] {
            { c1, b1 },
            { c2, b2 }
        });
            var matrixY = Matrix<double>.Build.DenseOfArray(new[,] {
            { a1, c1 },
            { a2, c2 }
        });

            // 计算行列式
            double Dx = matrixX.Determinant();
            double Dy = matrixY.Determinant();
            interPoint = new PointF((float)(Dx/D),(float)(Dy/D));
            return true;
            //return (Dx / D, Dy / D);
        }

        // 输入系数：2x+3y=5, 4x+y=6  SolveLinearEquations(2,3,5,4,1,6);
        public static bool SolveLinearEquations(double a1, double b1, double c1, double a2, double b2, double c2,out double x,out double y)
        {        
            // 输入系数：2x+3y=5, 4x+y=6  SolveLinearEquations(2,3,5,4,1,6);
            x = 0.0;
            y = 0.0;
            double det = a1 * b2 - a2 * b1; // 系数矩阵行列式
            if (det == 0)
            {
                //Console.WriteLine("方程组无唯一解或无解");
                return false;
            }

            x = (c1 * b2 - c2 * b1) / det;
            y = (a1 * c2 - a2 * c1) / det;
            return true;
        }

    }
}
